{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.utils.data\n",
    "from torch import nn\n",
    "from torch.nn import functional as F\n",
    "\n",
    "from ignite.engine import Events, Engine\n",
    "from ignite.metrics import Accuracy, Loss\n",
    "\n",
    "import numpy as np\n",
    "import sklearn.datasets\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "sns.set()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Model_bilinear(nn.Module):\n",
    "    def __init__(self, features, num_embeddings):\n",
    "        super().__init__()\n",
    "        \n",
    "        self.gamma = 0.99\n",
    "        self.sigma = 0.3\n",
    "        \n",
    "        embedding_size = 10\n",
    "        \n",
    "        self.fc1 = nn.Linear(2, features)\n",
    "        self.fc2 = nn.Linear(features, features)\n",
    "        self.fc3 = nn.Linear(features, features)\n",
    "        \n",
    "        self.W = nn.Parameter(torch.normal(torch.zeros(embedding_size, num_embeddings, features), 1))\n",
    "        \n",
    "        self.register_buffer('N', torch.ones(num_embeddings) * 20)\n",
    "        self.register_buffer('m', torch.normal(torch.zeros(embedding_size, num_embeddings), 1))\n",
    "        \n",
    "        self.m = self.m * self.N.unsqueeze(0)\n",
    "\n",
    "    def embed(self, x):\n",
    "        x = F.relu(self.fc1(x))\n",
    "        x = F.relu(self.fc2(x))\n",
    "        x = self.fc3(x)\n",
    "        \n",
    "        # i is batch, m is embedding_size, n is num_embeddings (classes)\n",
    "        x = torch.einsum('ij,mnj->imn', x, self.W)\n",
    "        \n",
    "        return x\n",
    "\n",
    "    def bilinear(self, z):\n",
    "        embeddings = self.m / self.N.unsqueeze(0)\n",
    "        \n",
    "        diff = z - embeddings.unsqueeze(0)            \n",
    "        y_pred = (- diff**2).mean(1).div(2 * self.sigma**2).exp()\n",
    "\n",
    "        return y_pred\n",
    "\n",
    "    def forward(self, x):\n",
    "        z = self.embed(x)\n",
    "        y_pred = self.bilinear(z)\n",
    "        \n",
    "        return z, y_pred\n",
    "\n",
    "    def update_embeddings(self, x, y):\n",
    "        z = self.embed(x)\n",
    "        \n",
    "        # normalizing value per class, assumes y is one_hot encoded\n",
    "        self.N = torch.max(self.gamma * self.N + (1 - self.gamma) * y.sum(0), torch.ones_like(self.N))\n",
    "        \n",
    "        # compute sum of embeddings on class by class basis\n",
    "        features_sum = torch.einsum('ijk,ik->jk', z, y)\n",
    "        \n",
    "        self.m = self.gamma * self.m + (1 - self.gamma) * features_sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(0)\n",
    "torch.manual_seed(0)\n",
    "\n",
    "l_gradient_penalty = 1.0\n",
    "\n",
    "# Moons\n",
    "noise = 0.1\n",
    "X_train, y_train = sklearn.datasets.make_moons(n_samples=1500, noise=noise)\n",
    "X_test, y_test = sklearn.datasets.make_moons(n_samples=200, noise=noise)\n",
    "\n",
    "num_classes = 2\n",
    "batch_size = 64\n",
    "\n",
    "model = Model_bilinear(20, num_classes)\n",
    "\n",
    "optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9, weight_decay=1e-4)\n",
    "\n",
    "\n",
    "def calc_gradient_penalty(x, y_pred):\n",
    "    gradients = torch.autograd.grad(\n",
    "            outputs=y_pred,\n",
    "            inputs=x,\n",
    "            grad_outputs=torch.ones_like(y_pred),\n",
    "            create_graph=True,\n",
    "        )[0]\n",
    "\n",
    "\n",
    "    gradients = gradients.flatten(start_dim=1)\n",
    "    \n",
    "    # L2 norm\n",
    "    grad_norm = gradients.norm(2, dim=1)\n",
    "\n",
    "    # Two sided penalty\n",
    "    gradient_penalty = ((grad_norm - 1) ** 2).mean()\n",
    "    \n",
    "    # One sided penalty - down\n",
    "#     gradient_penalty = F.relu(grad_norm - 1).mean()\n",
    "\n",
    "    return gradient_penalty\n",
    "\n",
    "\n",
    "def output_transform_acc(output):\n",
    "    y_pred, y, x, z = output\n",
    "    \n",
    "    y = torch.argmax(y, dim=1)\n",
    "        \n",
    "    return y_pred, y\n",
    "\n",
    "\n",
    "def output_transform_bce(output):\n",
    "    y_pred, y, x, z = output\n",
    "\n",
    "    return y_pred, y\n",
    "\n",
    "\n",
    "def output_transform_gp(output):\n",
    "    y_pred, y, x, z = output\n",
    "\n",
    "    return x, y_pred\n",
    "\n",
    "\n",
    "def step(engine, batch):\n",
    "    model.train()\n",
    "    optimizer.zero_grad()\n",
    "    \n",
    "    x, y = batch\n",
    "    x.requires_grad_(True)\n",
    "    \n",
    "    z, y_pred = model(x)\n",
    "    \n",
    "    loss1 =  F.binary_cross_entropy(y_pred, y)\n",
    "    loss2 = l_gradient_penalty * calc_gradient_penalty(x, y_pred)\n",
    "    \n",
    "    loss = loss1 + loss2\n",
    "    \n",
    "    loss.backward()\n",
    "    optimizer.step()\n",
    "    \n",
    "    with torch.no_grad():\n",
    "        model.update_embeddings(x, y)\n",
    "    \n",
    "    return loss.item()\n",
    "\n",
    "\n",
    "def eval_step(engine, batch):\n",
    "    model.eval()\n",
    "\n",
    "    x, y = batch\n",
    "\n",
    "    x.requires_grad_(True)\n",
    "\n",
    "    z, y_pred = model(x)\n",
    "\n",
    "    return y_pred, y, x, z\n",
    "    \n",
    "\n",
    "trainer = Engine(step)\n",
    "evaluator = Engine(eval_step)\n",
    "\n",
    "metric = Accuracy(output_transform=output_transform_acc)\n",
    "metric.attach(evaluator, \"accuracy\")\n",
    "\n",
    "metric = Loss(F.binary_cross_entropy, output_transform=output_transform_bce)\n",
    "metric.attach(evaluator, \"bce\")\n",
    "\n",
    "metric = Loss(calc_gradient_penalty, output_transform=output_transform_gp)\n",
    "metric.attach(evaluator, \"gp\")\n",
    "\n",
    "\n",
    "ds_train = torch.utils.data.TensorDataset(torch.from_numpy(X_train).float(), F.one_hot(torch.from_numpy(y_train)).float())\n",
    "dl_train = torch.utils.data.DataLoader(ds_train, batch_size=batch_size, shuffle=True, drop_last=True)\n",
    "\n",
    "ds_test = torch.utils.data.TensorDataset(torch.from_numpy(X_test).float(), F.one_hot(torch.from_numpy(y_test)).float())\n",
    "dl_test = torch.utils.data.DataLoader(ds_test, batch_size=200, shuffle=False)\n",
    "\n",
    "@trainer.on(Events.EPOCH_COMPLETED)\n",
    "def log_results(trainer):\n",
    "    evaluator.run(dl_test)\n",
    "    metrics = evaluator.state.metrics\n",
    "\n",
    "    print(\"Test Results - Epoch: {} Acc: {:.4f} BCE: {:.2f} GP {:.2f}\"\n",
    "          .format(trainer.state.epoch, metrics['accuracy'], metrics['bce'], metrics['gp']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "trainer.run(dl_train, max_epochs=30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "domain = 3\n",
    "x_lin = np.linspace(-domain+0.5, domain+0.5, 100)\n",
    "y_lin = np.linspace(-domain, domain, 100)\n",
    "\n",
    "xx, yy = np.meshgrid(x_lin, y_lin)\n",
    "\n",
    "X_grid = np.column_stack([xx.flatten(), yy.flatten()])\n",
    "\n",
    "X_vis, y_vis = sklearn.datasets.make_moons(n_samples=1000, noise=noise)\n",
    "mask = y_vis.astype(np.bool)\n",
    "\n",
    "with torch.no_grad():\n",
    "    output = model(torch.from_numpy(X_grid).float())[1]\n",
    "    confidence = output.max(1)[0].numpy()\n",
    "\n",
    "\n",
    "z = confidence.reshape(xx.shape)\n",
    "\n",
    "plt.figure()\n",
    "plt.contourf(x_lin, y_lin, z, cmap='cividis')\n",
    "\n",
    "plt.scatter(X_vis[mask,0], X_vis[mask,1])\n",
    "plt.scatter(X_vis[~mask,0], X_vis[~mask,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
